﻿using LyData.Data;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Admin_MenuList : System.Web.UI.Page
{
    protected void Bind()
    {
        IDb db = new DbFactory().DriverDb();
        db.Open();
        DataTable table = db.DataTable("MenuId, MenuName, FileName, Attributer, Location, Href, Target, Encrypt, Show, AddTime,UpTime,OrderId", "menu", string.Format("SiteLanguage={0} ", SiteInfo.SiteLanguage()), "Location ASC, OrderId ASC,MenuId ASC");
        db.Close();
        this.MenuList.DataSource = table;
        this.MenuList.DataBind();
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string str = this.MenuList.DataKeys[this.MenuList.EditIndex].Values[0].ToString();
        IDb db = new DbFactory().DriverDb();
        db.Open();
        int num = db.Scalar("count(FileName)", "menu", string.Format("FileName='{0}' AND MenuId<>{1}", args.Value.ToString(), str));
        db.Close();
        if (num > 0)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }

    protected void MenuList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.MenuList.PageIndex = e.NewPageIndex;
        this.Bind();
    }

    protected void MenuList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.MenuList.EditIndex = -1;
        this.Bind();
    }

    protected void MenuList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DropDownList list = (DropDownList)e.Row.FindControl("TargetList");
        if (list != null)
        {
            MenuProvider provider = new MenuProvider();
            list.DataSource = provider.Target;
            list.DataValueField = "key";
            list.DataTextField = "value";
            list.DataBind();
            list.SelectedValue = ((HiddenField)e.Row.FindControl("TargetHiddenField")).Value;
            if (((HiddenField)e.Row.FindControl("AttributerHiddenField")).Value == "901")
            {
                ((CheckBox)e.Row.Cells[5].Controls[0]).Visible = false;
                ((CheckBox)e.Row.Cells[6].Controls[0]).Visible = false;
            }
            else
            {
                ((TextBox)e.Row.Cells[8].Controls[0]).Visible = false;
            }
        }
        Button button = (Button)e.Row.FindControl("EditBt");
        Button control = (Button)e.Row.FindControl("DelBt");
        if (button != null)
        {
            if (!ManagerLogin.ValidateRole("C_MenuList.aspx_a"))
            {
                button.Enabled = false;
            }
            if (!ManagerLogin.ValidateRole("C_MenuList.aspx_b"))
            {
                control.Enabled = false;
            }
            else
            {
                ClientScriptManager clientScript = this.Page.ClientScript;
                string str2 = "";
                str2 = clientScript.GetPostBackEventReference(control, "").Replace("'", @"\'");
                control.OnClientClick = "return myConfirm('确认操作','将同时删除此栏目、所有子栏目、及其栏目内所有内容<br />确定删除吗?','" + str2 + "');";
            }
        }
    }

    protected void MenuList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int rowIndex = e.RowIndex;
        string str = this.MenuList.DataKeys[rowIndex].Values[0].ToString();
        IDb db = new DbFactory().DriverDb();
        db.Open();
        string str3 = ((HiddenField)this.MenuList.Rows[rowIndex].FindControl("AttributerHiddenField")).Value;
        if (str3 != null)
        {
            if (!(str3 == "0"))
            {
                if (str3 == "1")
                {
                    db.Delete("ArticleClass", string.Format("menuId={0}", str));
                    db.Delete("Article", string.Format("menuId={0}", str));
                }
                else if (str3 == "2")
                {
                    db.Delete("picClass", string.Format("menuId={0}", str));
                    db.Delete("pic", string.Format("menuId={0}", str));
                }
                else if (str3 == "3")
                {
                    db.Delete("ProductClass", string.Format("menuId={0}", str));
                    db.Delete("Product", string.Format("menuId={0}", str));
                    db.Delete("ProductConfig", string.Format("menuId={0}", str));
                }
                else if (str3 == "4")
                {
                    db.Delete("Message", string.Format("menuId={0}", str));
                    db.Delete("MessageConfig", string.Format("menuId={0}", str));
                }
            }
            else
            {
                db.Delete("SinglePage", string.Format("menuId={0}", str));
            }
        }
        db.Delete("Menu", string.Format("MenuId={0}", str));
        db.Close();
        this.Bind();
        this.Msg.Text = "栏目删除成功！";
        this.Msg.Visible = true;
        this.ContentMenu.Bind();
    }

    protected void MenuList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.MenuList.EditIndex = e.NewEditIndex;
        this.Bind();
    }

    protected void MenuList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int rowIndex = e.RowIndex;
        string str = ((TextBox)this.MenuList.Rows[rowIndex].Cells[0].Controls[0]).Text.ToString();
        string str2 = ((TextBox)this.MenuList.Rows[rowIndex].Cells[1].Controls[0]).Text.ToString();
        string str3 = ((TextBox)this.MenuList.Rows[rowIndex].FindControl("FileName")).Text.ToString();
        string str4 = ((TextBox)this.MenuList.Rows[rowIndex].Cells[8].Controls[0]).Text.ToString();
        string str5 = ((CheckBox)this.MenuList.Rows[rowIndex].Cells[5].Controls[0]).Checked.ToString();
        string str6 = ((CheckBox)this.MenuList.Rows[rowIndex].Cells[6].Controls[0]).Checked.ToString();
        string str7 = ((DropDownList)this.MenuList.Rows[rowIndex].FindControl("TargetList")).SelectedValue.ToString();
        IDb db = new DbFactory().DriverDb();
        db.Open();
        string[,] includeColumns = new string[,] { { "MenuName", str2, "NVarChar", "0" }, { "FileName", str3, "NVarChar", "32" }, { "OrderId", str, "Int", "0" }, { "Href", str4, "NvarChar", "0" }, { "Encrypt", str5, "Bit", "0" }, { "Show", str6, "Bit", "0" }, { "Target", str7, "Nvarchar", "32" }, { "UpTime", DateTime.Now.ToString(), "DateTime", "0" } };
        string str8 = this.MenuList.DataKeys[rowIndex].Values[0].ToString();
        string whereStr = string.Format("MenuId={0}", str8);
        db.Update("menu", includeColumns, whereStr);
        string str11 = ((HiddenField)this.MenuList.Rows[rowIndex].FindControl("AttributerHiddenField")).Value;
        if (str11 != null)
        {
            string[,] strArray2;
            if (!(str11 == "0"))
            {
                if (str11 == "1")
                {
                    strArray2 = new string[,] { { "ClassName", str2, "NVarChar", "0" }, { "FileName", str3, "NVarChar", "32" } };
                    whereStr = string.Format("MenuId={0} AND ParentId=0", str8);
                    db.Update("articleClass", strArray2, whereStr);
                }
                else if (str11 == "2")
                {
                    strArray2 = new string[,] { { "ClassName", str2, "NVarChar", "0" }, { "FileName", str3, "NVarChar", "32" } };
                    whereStr = string.Format("MenuId={0} AND ParentId=0", str8);
                    db.Update("picClass", strArray2, whereStr);
                }
                else if (str11 == "3")
                {
                    strArray2 = new string[,] { { "ClassName", str2, "NVarChar", "0" }, { "FileName", str3, "NVarChar", "32" } };
                    whereStr = string.Format("MenuId={0} AND ParentId=0", str8);
                    db.Update("productClass", strArray2, whereStr);
                }
                else if (str11 == "4")
                {
                    strArray2 = new string[,] { { "MenuName", str2, "NVarChar", "0" }, { "FileName", str3, "NVarChar", "32" } };
                    whereStr = string.Format("MenuId={0}", str8);
                    db.Update("messageConfig", strArray2, whereStr);
                }
            }
            else
            {
                strArray2 = new string[,] { { "Title", str2, "NVarChar", "0" }, { "FileName", str3, "NVarChar", "32" } };
                whereStr = string.Format("MenuId={0} AND ParentId=0", str8);
                db.Update("singlePage", strArray2, whereStr);
            }
        }
        db.Close();
        this.Msg.Text = "栏目编辑成功！";
        this.Msg.Visible = true;
        this.MenuList.EditIndex = -1;
        this.Bind();
        this.ContentMenu.Bind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Msg.Visible = false;
        if (!base.IsPostBack)
        {
            this.Bind();
        }
    }
}
